package com.chuanyang.driverh.mainhome;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.LogUtils;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class DriverJoinActivity extends BaseTitleActivity {

    private ListView lv_list;
    private EditText et_search;
    private ImageView iv_clear_text;

    @Override
    protected int getContentResId() {
        return R.layout.activity_driver_join;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setBackImg();
        getTitleTextView().setText("搜索");
        lv_list = (ListView) findViewById(R.id.lv_list);
        et_search = (EditText) findViewById(R.id.et_search);
        et_search.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        et_search.setInputType(EditorInfo.TYPE_CLASS_TEXT);
        et_search.setSingleLine(true);
        iv_clear_text = (ImageView) findViewById(R.id.iv_clear_text);
        iv_clear_text.setOnClickListener(this);
    }


    @Override
    protected void initData() {
        et_search.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    // 先隐藏键盘
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(DriverJoinActivity.this.getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    //进行搜索操作的方法，在该方法中可以加入mEditSearchUser的非空判断
                    if(et_search.getText().toString().trim() != null){
                        search(et_search.getText().toString().trim(),"1","10");
                    }else {
                        Toast.makeText(DriverJoinActivity.this,"请输入要搜索的内容",Toast.LENGTH_SHORT).show();
                    }
                }
                return false;
            }
        });
    }

    @Override
    protected void initListener() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_clear_text:
                et_search.setText("");
                break;
        }
    }

    private void search(String search,String page,String length){
        DialogUtil.showLoadingDialog(DriverJoinActivity.this,"加载中...");
        Map<String,Object> paramssss =new HashMap<String, Object>();
        paramssss.put("type", "1");//1：司机 2：车队长
        paramssss.put("fleetId", SharedPrefUtil.getFleetId());
        paramssss.put("search",search);
        paramssss.put("page",page);
        paramssss.put("length",length);

        MyHttpUtil.addBusinessRelation(DriverJoinActivity.this,paramssss, new MyCallback<Result>(
                DriverJoinActivity.this,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {

            }
            @Override
            public void onSuccess(Result result, int id) {

                LogUtils.d(result.getCode()+result.getMsg());

            }
        });
    }
}
